import { Component, OnInit } from '@angular/core';
import { DialogService } from 'app/shared/dialog';
import { NotificationService } from 'app/shared/service';
import { Subject } from 'rxjs';
import { UserManagementService } from '../user-management.service';
import { HttpClient } from '@angular/common/http';
import { PublicService } from 'app/public/public.service';

@Component({
    selector: 'itmp-manage-organization',
    templateUrl: './manage-organization.component.html',
    styleUrls: ['./manage-organization.component.css']
})
export class ManageOrganizationComponent implements OnInit {
    search$ = new Subject<object>();
    renderHeader = [];
    renderHeader1 = [];
    // 右上角的打印，放大等按钮
    toolbarList = [{ className: 'icon-dayinfuyin-' }];
    // pagination: any; // 分页数据的对象
    searchData: any; // 搜索数据的对象
    addUserTitle: string; // 新建或编辑的title
    listOfData = []; // 列表数据的数组
    listOfData1 = []; // 列表数据的数组
    selectedUser = null; // 每行tr的数据
    isVisible = false; // false为模态框隐藏
    isDeleteLoading = false;
    listOfOption = []; // 列表的数据
    roleOptions = [];
    userId = '';
    queryName = null;
    queryId = null;
    // organization分页
    pagination = {
        totalEle: 25,
        currentPage: 1,
        currentSize: 10,
        pageSize: 10
    };
    // access分页
    pagination1 = {
        totalEle1: 25,
        currentPage1: 1,
        currentSize1: 10,
        pageSize1: 10
    };
    accessButton = {
        Create: false,
        Edit: false,
        Delete: false,
        PRINT: false
    };
    constructor(
        private dialog: DialogService,
        private message: NotificationService,
        private http: HttpClient,
        private UMService: UserManagementService,
        private pub: PublicService
    ) {}

    ngOnInit() {
        // organization表头
        this.renderHeader = [
            {
                name: 'Organization_ID',
                key: null,
                value: 'organization_Id',
                isChecked: true
            },
            {
                name: 'Organization_Name',
                key: null,
                value: 'organization_Name',
                isChecked: true
            }
        ];
        // access表头
        this.renderHeader1 = [
            {
                name: 'Access_Right_Name',
                key: null,
                value: 'Access_Right_Name',
                isChecked: true
            },
            {
                name: 'Description',
                key: null,
                value: 'description',
                isChecked: true
            }
        ];
        this.getData(); // organization
        this.getData1(); // access
        this.initData();
        this.getButtonAccess();
    }
    getButtonAccess() {
        this.accessButton = this.pub.getButtonAccess('25', this.accessButton);
    }

    // 获取数据
    getData() {
        this.selectedUser = null;
        // 获取用户列表
        if (this.queryName === '' || this.queryId === '') {
            this.queryName = null;
            this.queryId = null;
        }
        const queryData = {
            condition: {
                organization_Id: this.queryId,
                organization_Name: this.queryName
            },
            pageNum: this.pagination.currentPage,
            pageSize: this.pagination.pageSize
        };
        // 获取organization
        this.UMService.getAllOrganizations(queryData).subscribe(res => {
            if (res.errCode === '000000') {
                this.listOfData = res.content.content;
                this.pagination.currentPage = res.content.pageNum;
                this.pagination.currentSize = res.content.numberOfElements;
                this.pagination.totalEle = res.content.totolElements;
            }
        });
    }
    getData1() {
        // 获取权限列表
        const queryData1 = {
            pageNum: this.pagination1.currentPage1,
            pageSize: this.pagination1.pageSize1
        };
        const query = this.UMService.getAllAccessRight(queryData1);
        query.subscribe(
            res => {
                this.listOfData1 = res.content.content;
                this.pagination1.currentPage1 = res.content.pageNum;
                this.pagination1.currentSize1 = res.content.numberOfElements;
                this.pagination1.totalEle1 = res.content.totolElements;
            },
            err => {}
        );
    }
    // 初始化分页和搜索
    initData() {
        this.searchData = {
            organization_Id: null,
            organization_Name: null
        };
    }
    // 新建或则编辑
    addOrEdit(flag) {
        if (flag === 'add') {
            this.addUserTitle = 'Add_Organization';
            // this.cancelActive();
            this.selectedUser = {
                organization_Id: 0,
                organization_Name: ''
            };
            this.isVisible = true;
        } else {
            this.addUserTitle = 'Edit_Organization';
            if (this.selectedUser) {
                this.isVisible = true;
            } else {
                // 提醒用户未选择要编辑的内容
                this.message.notify({ type: 'warning', path: 'UserManagement.ManageOrganization.select_notify' });
            }
        }
    }
    // 删除
    delete() {
        this.isDeleteLoading = false;
        if (this.selectedUser) {
            this.dialog
                .confirm({
                    title: 'UserManagement.ManageOrganization.Delete_Organization',
                    content: 'UserManagement.ManageOrganization.Delete_Content',
                    buttonOkTxt: 'UserManagement.ManageOrganization.Delete_Save',
                    buttonCancelTxt: 'UserManagement.ManageOrganization.Delete_Cancel'
                })
                .subscribe(res => {
                    if (res) {
                        this.confirmDelete();
                    } else {
                        this.cancelDelete();
                    }
                });
        } else {
            // 提醒用户未选择要删除的内容
            this.message.notify({ type: 'warning', path: 'UserManagement.ManageOrganization.delete_notify' });
        }
    }
    // 确认删除
    confirmDelete() {
        this.UMService.deleteOrganization(this.selectedUser.organization_Id).subscribe(
            res => {
                if (res.code === '900011') {
                    // organization正在使用
                    this.message.notify({ type: 'warning', path: 'UserManagement.ManageOrganization.fail_notify' });
                }
                this.refresh();
            },
            err => {}
        );
    }
    // 取消删除
    cancelDelete() {}
    // 刷新
    refresh(): void {
        // this.cancelActive();
        this.initData();
        this.pagination.currentPage = 1;
        this.pagination.pageSize = 10;
        this.pagination1.currentPage1 = 1;
        this.pagination1.pageSize1 = 10;
        this.queryName = null;
        this.queryId = null;
        this.getData();
        this.getData1();
    }
    // 点击tr取值
    click(i): void {
        this.addUserTitle = 'Edit_Organization';
        this.listOfData.forEach((data, index) => {
            data.active = false;
            if (i === index) {
                // 点击每个tr给与样式active
                data.active = true;
                const obj = {
                    organization_Id: data.organization_Id,
                    organization_Name: data.organization_Name
                };
                this.selectedUser = obj;
            }
        });
    }
    // 点击tr取值
    click1(i): void {
        this.listOfData1.forEach((data, index) => {
            data.active = false;
            if (i === index) {
                // 点击每个tr给与样式active
                data.active = true;
            }
        });
    }

    // 更改成功
    updateSuccess() {
        this.isVisible = false;
        this.refresh();
    }

    // organization分页控件改变事件
    onPageChange(e) {
        this.pagination.currentPage = e;
        this.getData();
    }
    onSizeChange(e) {
        this.pagination.pageSize = e;
        this.pagination.currentPage = 1;
        this.getData();
    }
    // access分页控件改变事件
    onPageChange1(e) {
        this.pagination1.currentPage1 = e;
        this.getData1();
    }
    onSizeChange1(e) {
        this.pagination1.pageSize1 = e;
        this.pagination1.currentPage1 = 1;
        this.getData1();
    }
    onChange(result: Date): void {}
    onOk(result: Date): void {}
    filterSearch() {
        this.pagination.currentPage = 1;
        this.pagination.currentSize = 10;
        this.getData();
    }
}
